package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.fastgpt.vo.FastGptFileContentRespVO;
import com.ruoyi.system.fastgpt.vo.FastGptFileDetailRespVO;
import com.ruoyi.system.fastgpt.vo.FastGptFileListRespVO;
import com.ruoyi.system.fastgpt.vo.FastGptFileUrlRespVO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysWhitePaperKnowledgeBaseMapper;
import com.ruoyi.system.domain.SysWhitePaperKnowledgeBase;
import com.ruoyi.system.service.ISysWhitePaperKnowledgeBaseService;

/**
 * 白皮书知识库Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-25
 */
@Service
public class SysWhitePaperKnowledgeBaseServiceImpl implements ISysWhitePaperKnowledgeBaseService
{
    @Autowired
    private SysWhitePaperKnowledgeBaseMapper sysWhitePaperKnowledgeBaseMapper;

    @Value("${knowledge-base.preview-url}")
    private String previewUrl;

    @Override
    public List<FastGptFileListRespVO> listFastGptFile() {
        List<FastGptFileListRespVO> fastGptFileListRespVOList = Lists.newArrayList();

        // 查询知识库列表
        SysWhitePaperKnowledgeBase sysWhitePaperKnowledgeBase = new SysWhitePaperKnowledgeBase();
        List<SysWhitePaperKnowledgeBase> whitePaperKnowledgeBaseList = sysWhitePaperKnowledgeBaseMapper.selectSysWhitePaperKnowledgeBaseList(sysWhitePaperKnowledgeBase);

        if (CollectionUtils.isNotEmpty(whitePaperKnowledgeBaseList)) {
            Date currentDate = DateUtils.getNowDate();

            for (SysWhitePaperKnowledgeBase whitePaperKnowledgeBase:whitePaperKnowledgeBaseList) {
                FastGptFileListRespVO fastGptFileListRespVO = new FastGptFileListRespVO();
                fastGptFileListRespVO.setId(whitePaperKnowledgeBase.getId().toString());
                fastGptFileListRespVO.setType("file");
                fastGptFileListRespVO.setName(whitePaperKnowledgeBase.getOriginFileName());
                fastGptFileListRespVO.setCreateTime(currentDate);
                fastGptFileListRespVO.setUpdateTime(currentDate);

                fastGptFileListRespVOList.add(fastGptFileListRespVO);
            }
        }

        return fastGptFileListRespVOList;
    }

    @Override
    public FastGptFileContentRespVO getFastGptFileContent(Long id) {
        // 根据ID获取知识库信息
        SysWhitePaperKnowledgeBase whitePaperKnowledgeBase = sysWhitePaperKnowledgeBaseMapper.selectSysWhitePaperKnowledgeBaseById(id);

        FastGptFileContentRespVO fastGptFileContentRespVO = new FastGptFileContentRespVO();
        if (whitePaperKnowledgeBase != null) {
            fastGptFileContentRespVO.setTitle(whitePaperKnowledgeBase.getOriginFileName());
            fastGptFileContentRespVO.setPreviewUrl(previewUrl + whitePaperKnowledgeBase.getFileUrl());
        }

        return fastGptFileContentRespVO;
    }

    @Override
    public FastGptFileUrlRespVO getFastGptFileUrl(Long id) {
        // 根据ID获取知识库信息
        SysWhitePaperKnowledgeBase whitePaperKnowledgeBase = sysWhitePaperKnowledgeBaseMapper.selectSysWhitePaperKnowledgeBaseById(id);

        FastGptFileUrlRespVO fastGptFileUrlRespVO = new FastGptFileUrlRespVO();
        if (whitePaperKnowledgeBase != null) {
            fastGptFileUrlRespVO.setUrl(previewUrl + whitePaperKnowledgeBase.getFileUrl());
        }

        return fastGptFileUrlRespVO;
    }

    @Override
    public FastGptFileDetailRespVO getFastGptFileDetail(Long id) {
        // 根据ID获取知识库信息
        SysWhitePaperKnowledgeBase whitePaperKnowledgeBase = sysWhitePaperKnowledgeBaseMapper.selectSysWhitePaperKnowledgeBaseById(id);

        FastGptFileDetailRespVO fastGptFileDetailRespVO = new FastGptFileDetailRespVO();
        if (whitePaperKnowledgeBase != null) {
            fastGptFileDetailRespVO.setId(whitePaperKnowledgeBase.getId().toString());
            fastGptFileDetailRespVO.setName(whitePaperKnowledgeBase.getOriginFileName());
        }

        return fastGptFileDetailRespVO;
    }

    /**
     * 查询白皮书知识库
     *
     * @param id 白皮书知识库主键
     * @return 白皮书知识库
     */
    @Override
    public SysWhitePaperKnowledgeBase selectSysWhitePaperKnowledgeBaseById(Long id)
    {
        return sysWhitePaperKnowledgeBaseMapper.selectSysWhitePaperKnowledgeBaseById(id);
    }

    /**
     * 查询白皮书知识库列表
     *
     * @param sysWhitePaperKnowledgeBase 白皮书知识库
     * @return 白皮书知识库
     */
    @Override
    public List<SysWhitePaperKnowledgeBase> selectSysWhitePaperKnowledgeBaseList(SysWhitePaperKnowledgeBase sysWhitePaperKnowledgeBase)
    {
        return sysWhitePaperKnowledgeBaseMapper.selectSysWhitePaperKnowledgeBaseList(sysWhitePaperKnowledgeBase);
    }

    /**
     * 新增白皮书知识库
     *
     * @param sysWhitePaperKnowledgeBase 白皮书知识库
     * @return 结果
     */
    @Override
    public int insertSysWhitePaperKnowledgeBase(SysWhitePaperKnowledgeBase sysWhitePaperKnowledgeBase)
    {
        sysWhitePaperKnowledgeBase.setCreateTime(DateUtils.getNowDate());
        return sysWhitePaperKnowledgeBaseMapper.insertSysWhitePaperKnowledgeBase(sysWhitePaperKnowledgeBase);
    }

    @Override
    public void batchInsertSysWhitePaperKnowledgeBase(List<SysWhitePaperKnowledgeBase> whitePaperKnowledgeBaseList) {
        for (SysWhitePaperKnowledgeBase knowledgeBase:whitePaperKnowledgeBaseList) {
            sysWhitePaperKnowledgeBaseMapper.insertSysWhitePaperKnowledgeBase(knowledgeBase);
        }
//        sysWhitePaperKnowledgeBaseMapper.batchInsertSysWhitePaperKnowledgeBase(whitePaperKnowledgeBaseList);
    }

    /**
     * 修改白皮书知识库
     *
     * @param sysWhitePaperKnowledgeBase 白皮书知识库
     * @return 结果
     */
    @Override
    public int updateSysWhitePaperKnowledgeBase(SysWhitePaperKnowledgeBase sysWhitePaperKnowledgeBase)
    {
        sysWhitePaperKnowledgeBase.setUpdateTime(DateUtils.getNowDate());
        return sysWhitePaperKnowledgeBaseMapper.updateSysWhitePaperKnowledgeBase(sysWhitePaperKnowledgeBase);
    }

    /**
     * 批量删除白皮书知识库
     *
     * @param ids 需要删除的白皮书知识库主键
     * @return 结果
     */
    @Override
    public int deleteSysWhitePaperKnowledgeBaseByIds(Long[] ids)
    {
        return sysWhitePaperKnowledgeBaseMapper.deleteSysWhitePaperKnowledgeBaseByIds(ids);
    }

    /**
     * 删除白皮书知识库信息
     *
     * @param id 白皮书知识库主键
     * @return 结果
     */
    @Override
    public int deleteSysWhitePaperKnowledgeBaseById(Long id)
    {
        return sysWhitePaperKnowledgeBaseMapper.deleteSysWhitePaperKnowledgeBaseById(id);
    }

    @Override
    public int deleteSysWhitePaperKnowledgeBaseByProjectId(Long projectId) {
        return sysWhitePaperKnowledgeBaseMapper.deleteSysWhitePaperKnowledgeBaseByProjectId(projectId);
    }
}
